﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Metro.Kashikoi.ServiceContract.Item;
using Metro.Kashikoi.ServiceContract;
using Metro.Kashikoi.Common.ExceptionHandling;

namespace Metro.Kashikoi.Web.Controllers
{
    public class ItemController : Controller
    {
        IItemService itemService = ServiceBus.GetService<IItemService>();
        ICategoryService catService = ServiceBus.GetService<ICategoryService>();
        IVendorService venderService = ServiceBus.GetService<IVendorService>();


        //
        // GET: /Item/
        public ActionResult Index()
        {
            var items = itemService.GetAllItems();
            var itemsWithChildren = items.Select(it => itemService.GetItem(it.Id)).ToList();

            return View(itemsWithChildren);
        }

        //
        // GET: /Item/Details/5

        public ActionResult Details(int id)
        {
            var item = itemService.GetItem(id);
            return View(item);
        }

        //
        // GET: /Item/Create

        public ActionResult Create()
        {
            ViewBag.CategoryList = catService.GetAllCategories();
            return View();
        }

        //
        // POST: /Item/Create

        [HttpPost]
        public ActionResult Create(StockItem newItem, FormCollection form)
        {
            try
            {
                // TODO: Add insert logic here
                int categoryId = int.Parse(form["categoryList"]);
                var cat = catService.GetCategory(categoryId);
                newItem.Category = cat;
                newItem.Vendor = venderService.GetAllVendors()[0];
                newItem.ChargeDepartment = itemService.GetAllChargeDepartments()[0];

                var createdItem = itemService.CreateItem(newItem);

                return RedirectToAction("Index");
            }
            catch (AppException ex)
            {
                ViewBag.Message = ex.Message;
                ViewBag.CategoryList = catService.GetAllCategories();
                return View();
            }
        }

        //
        // GET: /Item/Edit/5

        public ActionResult Edit(int id)
        {
            var curItem = itemService.GetItem(id);
            ViewBag.CategoryList = catService.GetAllCategories();
            return View(curItem);
        }

        //
        // POST: /Item/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection form)
        {
            var curItem = itemService.GetItem(id);
            UpdateModel(curItem);
            var categoryId = int.Parse(form["categoryList"]);
            curItem.Category.Id = categoryId;

            try
            {
                itemService.UpdateItem(curItem);
                return RedirectToAction("Index");
            }
            catch (AppException ex)
            {
                ViewBag.Message = ex.Message;
                ViewBag.CategoryList = catService.GetAllCategories();
                return View();
            }
        }

        //
        // GET: /Item/Delete/5

        public ActionResult Delete(int id)
        {
            var curItem = itemService.GetItem(id);
            return View(curItem);
        }

        //
        // POST: /Item/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                itemService.DeleteItem(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
